package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.items.entity.Zysb;
import com.gfxy.modules.items.entity.teachingresult.TeachingResultContent;
import com.gfxy.modules.items.mapper.ZysbMapper;
import com.gfxy.modules.items.service.IZysbService;
import com.gfxy.modules.items.vo.*;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 新专业申报
 * @Author: jeecg-boot
 * @Date:   2021-08-29
 * @Version: V1.0
 */
@Service
public class ZysbServiceImpl extends AbstractProjectServiceImpl<ZysbMapper, Zysb> implements IZysbService {

	protected int memberCellNum = 3;
	@Override
	protected void pass(String id, String isPass) {

	}

	@Override
	protected String[] getMemberArray(Row row) {
		String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
		return nameStrs.split(memberNameSeparator);
	}

	@Override
	protected Zysb getOneProject(Row row) {
		String year = ExcelUtil.formatCell(row.getCell(0));
		String projectName = ExcelUtil.formatCell(row.getCell(1));
		String status = ExcelUtil.formatCell(row.getCell(2));


		Zysb zysb = new Zysb();
		zysb.setDate(year);
		zysb.setProjectName(projectName);
		zysb.setApplyStatus(translateStatus(status));
		zysb.setIsPass(0);
		zysb.setTotalScore(this.getTotalScore(zysb));
		return zysb;
	}

	protected double computeProportion(int ranked, int totalNum){
		if(totalNum==1){
			return 1.0;
		} else if(totalNum == 2){
			if(ranked ==1){
				return 0.6;
			} else if(ranked == 2){
				return 0.4;
			} else {
				return 0.0;
			}
		} else if(totalNum == 3){
			if(ranked ==1){
				return 0.5;
			} else if(ranked == 2){
				return 0.3;
			} else if(ranked == 3){
				return 0.2;
			} else {
				return 0.0;
			}
		} else if(totalNum == 4){
			if(ranked ==1){
				return 0.5;
			} else if(ranked == 2){
				return 0.25;
			} else if(ranked == 3){
				return 0.15;
			} else if(ranked == 4){
				return 0.1;
			} else {
				return 0.0;
			}
		} else if(totalNum == 5){
			if(ranked ==1){
				return 0.4;
			} else if(ranked == 2){
				return 0.2;
			} else if(ranked == 3){
				return 0.15;
			} else if(ranked == 4){
				return 0.1;
			} else if(ranked == 5){
				return 0.1;
			} else {
				return 0.0;
			}
		} else if(totalNum == 6){
			if(ranked ==1){
				return 0.4;
			} else if(ranked == 2){
				return 0.2;
			} else if(ranked == 3){
				return 0.15;
			} else if(ranked == 4){
				return 0.1;
			} else if(ranked == 5){
				return 0.05;
			} else if(ranked == 6){
				return 0.05;
			}else {
				return 0.0;
			}
		} else if(totalNum >=6){
			if(ranked == 1){
				return 0.4;
			} else if(ranked == 2){
				return 0.2;
			} else if(ranked ==3){
				return 0.15;
			} else if(ranked > 3){
				return Math.floor(0.2*10000/(totalNum-1))/10000.0 ;
			} else {
				return 0.0;
			}
		} else {
			return 0.0;
		}
	}


	@Override
	public Double getTotalScore(Zysb zysb) {
		double result = 0.0;
		int applyStatus = zysb.getApplyStatus();
		if (applyStatus == 1) {
		    result = 10;
		}else if (applyStatus == 2) {
		    result = 25;
		}
		return result;
	}




	private int translateStatus(String name){
		if(name == null || name.equals("")){
			return 0;
		} else if(name.equals("立项申报")){
			return 1;
		} else if(name.equals("申报成功")){
			return 2;
		} else {
			return 0;
		}
	}

	@Override
	public IPage<ZysbMemberPage> getZysbMemberPageListByYear(String date, Integer pageNo, Integer pageSize) {
		Page<ZysbMemberPage> page = new Page<>(pageNo,pageSize);
		return page.setRecords(this.baseMapper.selectZysbMemberPageListByYear(date));
	}

	@Override
	public List<ZysbMemberPage> getZysbMemberPageListByYear(String date) {
		return this.baseMapper.selectZysbMemberPageListByYear(date);
	}


	@Override
	protected List<TeachingResultContent> createTeachingResultContent(Zysb jxcg)  {

		List<TeachingResultContent> result = new ArrayList<>();

		// 类型
		TeachingResultContent classTeachingResultContent = new TeachingResultContent();
		classTeachingResultContent.setType(1);
		classTeachingResultContent.setLevel(jxcg.getApplyStatus());

		try {
			classTeachingResultContent.setValue(dictTranslater.getDictValue(Zysb.class, "applyStatus", String.valueOf(jxcg.getApplyStatus())));
			result.add(classTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		return result;
	}
}
